﻿package org.xhs.report.common.adv
{
    import baidu.lib.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class AdvLoader extends Sprite
    {
        private var isAvailable:Boolean = false;
        private var advLoader:Loader;
        private var unpopTimer:Timer;
        private var countDownTimer:Timer;
        private var _advDuration:Number = 2;
        private var _advImageUrl:String = "";
        private var _advLinkUrl:String = "";
        private var _advWidth:Number;
        protected var isAdvShown:Boolean = false;
        public static const START_LOADING:String = "start_loading";
        public static const FINISH_LOADING:String = "finish_loading";
        public static const FAIL_LOADING:String = "fail_to_load";
        public static const ADV_CLICK:String = "adv_click";
        public static const ADV_VANISH:String = "adv_vanish";

        public function AdvLoader()
        {
            this.advLoader = new Loader();
            this.advLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imgComplete);
            this.advLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.imgError);
            this.advLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.imgError);
            this.unpopTimer = new Timer(this._advDuration * 1000, 1);
            this.unpopTimer.addEventListener(TimerEvent.TIMER, this.unpopAdv);
            return;
        }// end function

        protected function imgComplete(event:Event) : void
        {
            if (!isNaN(this._advWidth) && this.advLoader.width)
            {
                var _loc_2:int = 1;
                this.advLoader.scaleY = 1;
                this.advLoader.scaleX = _loc_2;
                var _loc_2:* = this._advWidth / this.advLoader.width;
                this.advLoader.scaleY = this._advWidth / this.advLoader.width;
                this.advLoader.scaleX = _loc_2;
            }
            this.isAvailable = true;
            dispatchEvent(new Event(AdvLoader.FINISH_LOADING));
            if (this._advLinkUrl)
            {
                buttonMode = true;
                addEventListener(MouseEvent.MOUSE_DOWN, this.imgDown);
            }
            return;
        }// end function

        protected function imgError(event:Event) : void
        {
            trace("fail to load image.");
            dispatchEvent(new Event(AdvLoader.FAIL_LOADING));
            return;
        }// end function

        protected function imgDown(event:MouseEvent) : void
        {
            dispatchEvent(new Event(AdvLoader.ADV_CLICK));
            if (this._advLinkUrl)
            {
                BrowserUtil.openNewWindow(this._advLinkUrl);
            }
            return;
        }// end function

        public function popAdv() : void
        {
            if (!this.isAdvShown)
            {
                this.advLoader.unloadAndStop();
                this.advLoader.load(new URLRequest(this._advImageUrl));
                dispatchEvent(new Event(AdvLoader.START_LOADING));
                addChild(this.advLoader);
                this.unpopTimer.reset();
                this.unpopTimer.start();
                this.isAdvShown = true;
                this.isAvailable = false;
            }
            else
            {
                this.unpopAdv(null);
                this.popAdv();
            }
            return;
        }// end function

        public function unpopAdv(event:TimerEvent = null) : void
        {
            if (this.isAdvShown)
            {
                this.isAdvShown = false;
                if (contains(this.advLoader))
                {
                    removeChild(this.advLoader);
                }
                this.unpopTimer.reset();
                dispatchEvent(new Event(AdvLoader.ADV_VANISH));
            }
            return;
        }// end function

        public function get advDuration() : Number
        {
            return this._advDuration;
        }// end function

        public function set advDuration(param1:Number) : void
        {
            if (param1 <= 0)
            {
                throw new Error("Adv. Duration Error");
            }
            this._advDuration = param1;
            this.unpopTimer.delay = this._advDuration * 1000;
            return;
        }// end function

        public function get advImageUrl() : String
        {
            return this._advImageUrl;
        }// end function

        public function set advImageUrl(param1:String) : void
        {
            this._advImageUrl = param1;
            return;
        }// end function

        public function get advLinkUrl() : String
        {
            return this._advLinkUrl;
        }// end function

        public function set advLinkUrl(param1:String) : void
        {
            this._advLinkUrl = param1;
            return;
        }// end function

        public function get advWidth() : Number
        {
            return this._advWidth;
        }// end function

        public function set advWidth(param1:Number) : void
        {
            this._advWidth = param1;
            return;
        }// end function

    }
}
